//
//  SampleData.swift
//  AppleDemo
//
//  Created by Just 花语 on 2025/10/28.
//

import Foundation
import SwiftData

class SampleData{
    static let shared:SampleData = SampleData()
    let modelContainer :ModelContainer
    var context: ModelContext{
        modelContainer.mainContext
    }
    var friend:FriendIn5{
        FriendIn5.sampleData.first!
    }
    var movie:Movie{
        Movie.sampleData.first!
    }
    private init(){
        let schema = Schema([
            FriendIn5.self,
            Movie.self
        ])
        let modelConfiguration = ModelConfiguration(schema:schema, isStoredInMemoryOnly:true)
        do{
            modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
            insertSampleData()
            try context.save()
        }catch{
            fatalError("Could not create ModelContainer: \(error)")
        }
    }
    private func insertSampleData(){
        for friend in FriendIn5.sampleData {
           context.insert(friend)
       }
        for movie in Movie.sampleData {
            context.insert(movie)
        }
        FriendIn5.sampleData[0].favoriteMovie = Movie.sampleData[1]
        FriendIn5.sampleData[2].favoriteMovie = Movie.sampleData[0]
        FriendIn5.sampleData[3].favoriteMovie = Movie.sampleData[4]
        FriendIn5.sampleData[4].favoriteMovie = Movie.sampleData[0]
    }
}
